package com.woniuxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.woniuxy.commons.utile.Result;
import com.woniuxy.controller.dto.AccountDto;
import com.woniuxy.controller.dto.RepairMaterialGetDto;
import com.woniuxy.controller.form.FirstForm;
import com.woniuxy.controller.form.MaterialForm;
import com.woniuxy.controller.form.RepairMaterialForm;
import com.woniuxy.dao.RepairAttachmentDao;
import com.woniuxy.dao.model.RepairAttachment;
import com.woniuxy.dao.model.RepairMaterial;
import com.woniuxy.dao.RepairMaterialDao;
import com.woniuxy.service.RepairAttachmentService;
import com.woniuxy.service.RepairMaterialService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 维修用料表 服务实现类
 * </p>
 *
 * @author Keep it simple
 * @since 2022-06-28
 */
@Service
public class RepairMaterialServiceImpl extends ServiceImpl<RepairMaterialDao, RepairMaterial> implements RepairMaterialService {

    @Resource
    private RepairMaterialDao repairMaterialDao;

    @Resource
    private RepairAttachmentService attachmentService;

    @Resource
    private RepairMaterialService repairMaterialService;
    /**
     * 新增材料用料表
     * @param repairMaterialForm
     */
    @Override
    public void addMaterial(RepairMaterialForm repairMaterialForm) {
        AccountDto accountDto = (AccountDto)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String userId = accountDto.getAccount();
        String repairnum = repairMaterialForm.getRepairnum();
        List<RepairMaterial> entities = new ArrayList<>();
        RepairMaterial first = new RepairMaterial();
        first.setRepairnum(repairnum);
        first.setCount(repairMaterialForm.getFirst().getCount());
        first.setMaterialcode(repairMaterialForm.getFirst().getDitcode());
        first.setCreateuser(userId);
        entities.add(first);
        for(FirstForm add : repairMaterialForm.getAdds()){
            RepairMaterial buff = new RepairMaterial();
            buff.setRepairnum(repairnum);
            buff.setCount(add.getCount());
            buff.setMaterialcode(add.getDitcode());
            buff.setCreateuser(userId);
            entities.add(buff);
        }
        System.out.println(entities);
        repairMaterialService.saveBatch(entities);
        //将维修完成的图片地址写到附件表中
        System.out.println(repairMaterialForm.getFilePath());
        UpdateWrapper<RepairAttachment> wrapper = new UpdateWrapper<>();
        wrapper.set("fileName",repairMaterialForm.getFilePath());
        wrapper.eq("repairNum",repairMaterialForm.getRepairnum());
        attachmentService.update(wrapper);
    }

    @Override
    public List<RepairMaterialGetDto> getRepairMaterial(String repairnum) {
        List<RepairMaterialGetDto> repairMaterial = repairMaterialDao.getRepairMaterial(repairnum);
        return repairMaterial;
    }
}
